package com.qinhao.loveCrowdFunding.controller;

import com.qinhao.loveCrowdFunding.enums.ResponseCodeEnum;
import com.qinhao.loveCrowdFunding.pojo.MessageStatusConvertDTO;
import com.qinhao.loveCrowdFunding.pojo.UserMessageDTO;
import com.qinhao.loveCrowdFunding.req.MessageStatusConvertByIdRequest;
import com.qinhao.loveCrowdFunding.req.MessageStatusConvertRequest;
import com.qinhao.loveCrowdFunding.req.UserMessageRequest;
import com.qinhao.loveCrowdFunding.resp.MessageStatusConvertResponse;
import com.qinhao.loveCrowdFunding.resp.UserMessageResponse;
import com.qinhao.loveCrowdFunding.service.MessageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;

@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    MessageService messageService;
    @RequestMapping("/user")
    public UserMessageResponse getUserMessage(@RequestBody UserMessageRequest userMessageRequest) {
        if (userMessageRequest.getId() == null) {
            return UserMessageResponse.builder()
                    .code(ResponseCodeEnum.USERIDISNULL.getCode())
                    .build();
        }
        UserMessageResponse userMessageResponse = UserMessageResponse.builder().build();
        try {
            UserMessageDTO userMessageDTO = messageService.getUserMessage(userMessageRequest);
            userMessageResponse.setCode(ResponseCodeEnum.SUCCESS.getCode());
            if (userMessageDTO != null) {
                BeanUtils.copyProperties(userMessageDTO, userMessageResponse);
            }
            // 修正read列表中时间的格式
            for (int i = 0; i < userMessageResponse.getReadMessage().size(); i++) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String date = sdf.format(sdf.parse(userMessageResponse.getReadMessage().get(i).getDate()));
                userMessageResponse.getReadMessage().get(i).setDate(date);
            }
            // 修正unread列表中时间的格式
            for (int i = 0; i < userMessageResponse.getUnreadMessage().size(); i++) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String date = sdf.format(sdf.parse(userMessageResponse.getUnreadMessage().get(i).getDate()));
                userMessageResponse.getUnreadMessage().get(i).setDate(date);
            }
            // 修正read列表中时间的格式
            for (int i = 0; i < userMessageResponse.getRecycleMessage().size(); i++) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String date = sdf.format(sdf.parse(userMessageResponse.getRecycleMessage().get(i).getDate()));
                userMessageResponse.getRecycleMessage().get(i).setDate(date);
            }
        } catch (Exception e) {
            userMessageResponse.setCode(ResponseCodeEnum.FAIL.getCode());
        }
        return userMessageResponse;
    }

    @RequestMapping("/unreadToReadById")
    public MessageStatusConvertResponse unRead2ReadById(@RequestBody MessageStatusConvertByIdRequest messageStatusConvertRequest) {
        if (messageStatusConvertRequest.getId() == null) {
            return MessageStatusConvertResponse.builder()
                    .code(ResponseCodeEnum.MESSAGEIDISNULL.getCode())
                    .build();
        }
        MessageStatusConvertResponse messageStatusConvertResponse = MessageStatusConvertResponse.builder().build();
        try {
            MessageStatusConvertDTO messageStatusConvertDTO = messageService.unRead2ReadById(messageStatusConvertRequest);
            if (messageStatusConvertDTO.getId() != null) {
                messageStatusConvertResponse.setCode(ResponseCodeEnum.SUCCESS.getCode());
            }else {
                messageStatusConvertResponse.setCode(ResponseCodeEnum.UNKNOW.getCode());
            }
        } catch (Exception e) {
           messageStatusConvertResponse.setCode(ResponseCodeEnum.FAIL.getCode());
        }
        return messageStatusConvertResponse;
    }

    @RequestMapping("/readToRecycleById")
    public MessageStatusConvertResponse read2RecycleById(@RequestBody MessageStatusConvertByIdRequest messageStatusConvertRequest) {
        if (messageStatusConvertRequest.getId() == null) {
            return MessageStatusConvertResponse.builder()
                    .code(ResponseCodeEnum.MESSAGEIDISNULL.getCode())
                    .build();
        }
        MessageStatusConvertResponse messageStatusConvertResponse = MessageStatusConvertResponse.builder().build();
        try {
            MessageStatusConvertDTO messageStatusConvertDTO = messageService.read2RecycleById(messageStatusConvertRequest);
            if (messageStatusConvertDTO.getId() != null) {
                messageStatusConvertResponse.setCode(ResponseCodeEnum.SUCCESS.getCode());
            }else {
                messageStatusConvertResponse.setCode(ResponseCodeEnum.UNKNOW.getCode());
            }
        } catch (Exception e) {
            messageStatusConvertResponse.setCode(ResponseCodeEnum.FAIL.getCode());
        }
        return messageStatusConvertResponse;
    }

    @RequestMapping("/recycleToReadById")
    public MessageStatusConvertResponse recycle2ReadById(@RequestBody MessageStatusConvertByIdRequest messageStatusConvertRequest) {
        if (messageStatusConvertRequest.getId() == null) {
            return MessageStatusConvertResponse.builder()
                    .code(ResponseCodeEnum.MESSAGEIDISNULL.getCode())
                    .build();
        }
        MessageStatusConvertResponse messageStatusConvertResponse = MessageStatusConvertResponse.builder().build();
        try {
            MessageStatusConvertDTO messageStatusConvertDTO = messageService.recycle2ReadById(messageStatusConvertRequest);
            if (messageStatusConvertDTO.getId() != null) {
                messageStatusConvertResponse.setCode(ResponseCodeEnum.SUCCESS.getCode());
            }else {
                messageStatusConvertResponse.setCode(ResponseCodeEnum.UNKNOW.getCode());
            }
        } catch (Exception e) {
            messageStatusConvertResponse.setCode(ResponseCodeEnum.FAIL.getCode());
        }
        return messageStatusConvertResponse;
    }

    @RequestMapping("/handleAllMessage")
    public MessageStatusConvertResponse handleAllMessage(@RequestBody MessageStatusConvertRequest messageStatusConvertRequest) {
        if (messageStatusConvertRequest.getState() == null) {
            return MessageStatusConvertResponse.builder()
                    .code(ResponseCodeEnum.MESSAGESTATEISNULL.getCode())
                    .build();
        }
        MessageStatusConvertResponse messageStatusConvertResponse = MessageStatusConvertResponse.builder().build();
        try {
            MessageStatusConvertDTO messageStatusConvertDTO = messageService.handleAllMessage(messageStatusConvertRequest);
            if (messageStatusConvertDTO.getId() != null) {
                messageStatusConvertResponse.setCode(ResponseCodeEnum.SUCCESS.getCode());
            }else {
                messageStatusConvertResponse.setCode(ResponseCodeEnum.UNKNOW.getCode());
            }
        } catch (Exception e) {
            messageStatusConvertResponse.setCode(ResponseCodeEnum.FAIL.getCode());
        }
        return messageStatusConvertResponse;
    }
}
